package org.example.ding.model;

import org.example.ding.entity.User;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * 营养预警模型
 */
public class NutritionAlert {
    
    private User user;
    private LocalDate date;
    private DailyNutritionSummary summary;
    private List<String> highPriorityAlerts;
    private List<String> mediumPriorityAlerts;
    private List<String> lowPriorityAlerts;
    
    public NutritionAlert(User user, DailyNutritionSummary summary) {
        this.user = user;
        this.summary = summary;
        this.date = summary.getDate();
        this.highPriorityAlerts = new ArrayList<>();
        this.mediumPriorityAlerts = new ArrayList<>();
        this.lowPriorityAlerts = new ArrayList<>();
        
        // 分析营养数据并生成预警
        analyzeNutrition();
    }
    
    /**
     * 分析营养数据并生成预警
     */
    private void analyzeNutrition() {
        analyzeCalories();
        analyzeProtein();
        analyzeCarbohydrates();
        analyzeFat();
    }
    
    /**
     * 分析热量摄入
     */
    private void analyzeCalories() {
        BigDecimal actual = summary.getTotalCalories();
        BigDecimal goal = user.getDailyCaloriesGoal();
        
        if (goal == null || goal.compareTo(BigDecimal.ZERO) == 0) {
            goal = new BigDecimal("2000"); // 默认目标
        }
        
        double ratio = actual.divide(goal, 4, BigDecimal.ROUND_HALF_UP).doubleValue();
        
        if (ratio >= 1.5) {
            highPriorityAlerts.add("热量摄入严重超标：已达到目标的" + String.format("%.1f", ratio * 100) + "%");
        } else if (ratio <= 0.5) {
            highPriorityAlerts.add("热量摄入严重不足：仅达到目标的" + String.format("%.1f", ratio * 100) + "%");
        } else if (ratio >= 1.3) {
            mediumPriorityAlerts.add("热量摄入超标：已达到目标的" + String.format("%.1f", ratio * 100) + "%");
        } else if (ratio <= 0.7) {
            mediumPriorityAlerts.add("热量摄入不足：仅达到目标的" + String.format("%.1f", ratio * 100) + "%");
        } else if (ratio >= 1.1) {
            lowPriorityAlerts.add("热量摄入略高：已达到目标的" + String.format("%.1f", ratio * 100) + "%");
        } else if (ratio <= 0.9) {
            lowPriorityAlerts.add("热量摄入略低：仅达到目标的" + String.format("%.1f", ratio * 100) + "%");
        }
    }
    
    /**
     * 分析蛋白质摄入
     */
    private void analyzeProtein() {
        BigDecimal actual = summary.getTotalProtein();
        BigDecimal goal = user.getDailyProteinGoal();
        
        if (goal == null || goal.compareTo(BigDecimal.ZERO) == 0) {
            goal = new BigDecimal("150"); // 默认目标
        }
        
        double ratio = actual.divide(goal, 4, BigDecimal.ROUND_HALF_UP).doubleValue();
        
        if (ratio <= 0.6) {
            highPriorityAlerts.add("蛋白质摄入严重不足：仅达到目标的" + String.format("%.1f", ratio * 100) + "%");
        } else if (ratio <= 0.8) {
            mediumPriorityAlerts.add("蛋白质摄入不足：仅达到目标的" + String.format("%.1f", ratio * 100) + "%");
        } else if (ratio <= 0.9) {
            lowPriorityAlerts.add("蛋白质摄入略低：仅达到目标的" + String.format("%.1f", ratio * 100) + "%");
        }
    }
    
    /**
     * 分析碳水化合物摄入
     */
    private void analyzeCarbohydrates() {
        BigDecimal actual = summary.getTotalCarbs();
        BigDecimal goal = user.getDailyCarbsGoal();
        
        if (goal == null || goal.compareTo(BigDecimal.ZERO) == 0) {
            goal = new BigDecimal("250"); // 默认目标
        }
        
        double ratio = actual.divide(goal, 4, BigDecimal.ROUND_HALF_UP).doubleValue();
        
        if (ratio >= 1.3) {
            mediumPriorityAlerts.add("碳水化合物摄入超标：已达到目标的" + String.format("%.1f", ratio * 100) + "%");
        } else if (ratio >= 1.1) {
            lowPriorityAlerts.add("碳水化合物摄入略高：已达到目标的" + String.format("%.1f", ratio * 100) + "%");
        }
    }
    
    /**
     * 分析脂肪摄入
     */
    private void analyzeFat() {
        BigDecimal actual = summary.getTotalFat();
        BigDecimal goal = user.getDailyFatGoal();
        
        if (goal == null || goal.compareTo(BigDecimal.ZERO) == 0) {
            goal = new BigDecimal("65"); // 默认目标
        }
        
        double ratio = actual.divide(goal, 4, BigDecimal.ROUND_HALF_UP).doubleValue();
        
        if (ratio >= 1.4) {
            highPriorityAlerts.add("脂肪摄入严重超标：已达到目标的" + String.format("%.1f", ratio * 100) + "%");
        } else if (ratio >= 1.2) {
            mediumPriorityAlerts.add("脂肪摄入超标：已达到目标的" + String.format("%.1f", ratio * 100) + "%");
        } else if (ratio >= 1.1) {
            lowPriorityAlerts.add("脂肪摄入略高：已达到目标的" + String.format("%.1f", ratio * 100) + "%");
        }
    }
    
    // Getter 方法
    public User getUser() {
        return user;
    }
    
    public LocalDate getDate() {
        return date;
    }
    
    public DailyNutritionSummary getSummary() {
        return summary;
    }
    
    public List<String> getHighPriorityAlerts() {
        return highPriorityAlerts;
    }
    
    public List<String> getMediumPriorityAlerts() {
        return mediumPriorityAlerts;
    }
    
    public List<String> getLowPriorityAlerts() {
        return lowPriorityAlerts;
    }
    
    /**
     * 是否有预警
     */
    public boolean hasAlerts() {
        return !highPriorityAlerts.isEmpty() || !mediumPriorityAlerts.isEmpty() || !lowPriorityAlerts.isEmpty();
    }
    
    /**
     * 获取总预警数量
     */
    public int getTotalAlertCount() {
        return highPriorityAlerts.size() + mediumPriorityAlerts.size() + lowPriorityAlerts.size();
    }
    
    /**
     * 获取高优先级预警数量
     */
    public int getHighPriorityCount() {
        return highPriorityAlerts.size();
    }
    
    /**
     * 获取中优先级预警数量
     */
    public int getMediumPriorityCount() {
        return mediumPriorityAlerts.size();
    }
    
    /**
     * 获取低优先级预警数量
     */
    public int getLowPriorityCount() {
        return lowPriorityAlerts.size();
    }
}
